home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / mipscoff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-22  |  56.0 KB  |  2,056 lines

  1. /* Read coff symbol tables and convert to internal format, for GDB.
  2.    Design and support routines derived from dbxread.c, and UMAX COFF
  3.    specific routines written 9/1/87 by David D. Johnson, Brown University.
  4.    Revised 11/27/87 ddj@cs.brown.edu
  5.    Copyright (C) 1987, 1988, 1989 Free Software Foundation, Inc.
  6.  
  7. This file is part of GDB.
  8.  
  9. GDB is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 1, or (at your option)
  12. any later version.
  13.  
  14. GDB is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with GDB; see the file COPYING.  If not, write to
  21. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23.  
  24. #include <a.out.h>
  25. #include <stdio.h>
  26. #include "symtab.h"
  27. #include <sys/file.h>
  28.  
  29. PDR *proc_desc_table = NULL;
  30. long proc_desc_length = 0;
  31.  
  32. static void add_symbol_to_list ();
  33. static int read_coff_symtab ();
  34. static void patch_opaque_types ();
  35. static struct type *decode_function_type ();
  36. static struct type *decode_type ();
  37. static struct type *decode_base_type ();
  38. static struct type *read_enum_type ();
  39. static struct type *read_struct_type ();
  40. static struct type *read_type ();
  41. static void finish_block ();
  42. static struct blockvector *make_blockvector ();
  43. static struct symbol *process_coff_symbol ();
  44. static char *getfilename ();
  45. static char *getsymname ();
  46.  
  47. extern int fclose ();
  48. extern void close ();
  49. extern void free_all_symtabs ();
  50. extern void free_all_psymtabs ();
  51.  
  52.  
  53. /* Name of source file whose symbol data we are now processing.
  54.    This comes from a symbol named ".file".  */
  55.  
  56. static char *last_source_file;
  57.  
  58. /* Core address of the end of the first object file.  */
  59. static CORE_ADDR first_object_file_end;
  60.  
  61. /* End of the text segment of the executable file,
  62.    as found in the symbol _etext.  */
  63.  
  64. static CORE_ADDR end_of_text_addr;
  65.  
  66. /* The end address of the last seen procedure. */
  67.  
  68. static CORE_ADDR last_end_addr;
  69.  
  70. /* The file, a.out  and text section headers of the symbol file */
  71.  
  72. static FILHDR file_hdr;
  73. static SCNHDR text_hdr;
  74. static AOUTHDR aout_hdr;
  75.  
  76. /* The index in the symbol table of the last coff symbol that was processed.  */
  77.  
  78. static int symnum;
  79.  
  80. /* Vector of types defined so far, indexed by their coff symnum.  */
  81.  
  82. static struct typevector *type_vector;
  83.  
  84. /* Number of elements allocated for type_vector currently.  */
  85.  
  86. static int type_vector_length;
  87.  
  88. /* Chain of typedefs of pointers to empty struct/union types.
  89.    They are chained thru the SYMBOL_VALUE.  */
  90.  
  91. #define HASHSIZE 127
  92. static struct symbol *opaque_type_chain[HASHSIZE];
  93.  
  94. /* Record the symbols defined for each context in a list.
  95.    We don't create a struct block for the context until we
  96.    know how long to make it.  */
  97.  
  98. struct pending
  99. {
  100.   struct pending *next;
  101.   struct symbol *symbol;
  102. };
  103.  
  104. /* Here are the three lists that symbols are put on.  */
  105.  
  106. struct pending *file_symbols;    /* static at top level, and types */
  107.  
  108. struct pending *global_symbols;    /* global functions and variables */
  109.  
  110. struct pending **global_symbols_all, **file_symbols_all;
  111.  
  112. struct pending *local_symbols;    /* everything local to lexical context */
  113.  
  114. /* List of unclosed lexical contexts
  115.    (that will become blocks, eventually).  */
  116.  
  117. struct context_stack
  118. {
  119.   struct context_stack *next;
  120.   struct pending *locals;
  121.   struct pending_block *old_blocks;
  122.   struct symbol *name;
  123.   CORE_ADDR start_addr;
  124. };
  125.  
  126. struct context_stack *context_stack;
  127.  
  128. /* Nonzero if within a function (so symbols should be local,
  129.    if nothing says specifically).  */
  130.  
  131. int within_function;
  132.  
  133. /* List of blocks already made (lexical contexts already closed).
  134.    This is used at the end to make the blockvector.  */
  135.  
  136. struct pending_block
  137. {
  138.   struct pending_block *next;
  139.   struct block *block;
  140. };
  141.  
  142. struct pending_block *pending_blocks;
  143.  
  144. extern CORE_ADDR startup_file_start;    /* From blockframe.c */
  145. extern CORE_ADDR startup_file_end;    /* From blockframe.c */
  146.  
  147. /* File name symbols were loaded from.  */
  148.  
  149. static char *symfile;
  150.  
  151. /* Look up a coff type-number index.  Return the address of the slot
  152.    where the type for that index is stored.
  153.    The type-number is in INDEX. 
  154.  
  155.    This can be used for finding the type associated with that index
  156.    or for associating a new type with the index.  */
  157.  
  158. static struct type **
  159. coff_lookup_type (index)
  160.      register int index;
  161. {
  162.   if (index >= type_vector_length)
  163.     {
  164.       int old_vector_length = type_vector_length;
  165.  
  166.       type_vector_length *= 2;
  167.       if (type_vector_length < index) {
  168.     type_vector_length = index * 2;
  169.       }
  170.       type_vector = (struct typevector *)
  171.     xrealloc (type_vector, sizeof (struct typevector)
  172.                 + type_vector_length * sizeof (struct type *));
  173.       bzero (&type_vector->type[ old_vector_length ],
  174.          (type_vector_length - old_vector_length) * sizeof(struct type *));
  175.     }
  176.   return &type_vector->type[index];
  177. }
  178.  
  179. /* Make sure there is a type allocated for type number index
  180.    and return the type object.
  181.    This can create an empty (zeroed) type object.  */
  182.  
  183. static struct type *
  184. coff_alloc_type (index)
  185.      int index;
  186. {
  187.   register struct type **type_addr = coff_lookup_type (index);
  188.   register struct type *type = *type_addr;
  189.  
  190.   /* If we are referring to a type not known at all yet,
  191.      allocate an empty type for it.
  192.      We will fill it in later if we find out how.  */
  193.   if (type == 0)
  194.     {
  195.       type = (struct type *) obstack_alloc (symbol_obstack,
  196.                         sizeof (struct type));
  197.       bzero (type, sizeof (struct type));
  198.       *type_addr = type;
  199.     }
  200.   return type;
  201. }
  202.  
  203. /* maintain the lists of symbols and blocks */
  204.  
  205. /* Add a symbol to one of the lists of symbols.  */
  206. static void
  207. add_symbol_to_list (symbol, listhead)
  208.      struct symbol *symbol;
  209.      struct pending **listhead;
  210. {
  211.   register struct pending *link
  212.     = (struct pending *) xmalloc (sizeof (struct pending));
  213.  
  214.   link->next = *listhead;
  215.   link->symbol = symbol;
  216.   *listhead = link;
  217. }
  218.  
  219. /* Take one of the lists of symbols and make a block from it.
  220.    Put the block on the list of pending blocks.  */
  221.  
  222. static void
  223. finish_block (symbol, listhead, old_blocks, start, end)
  224.      struct symbol *symbol;
  225.      struct pending **listhead;
  226.      struct pending_block *old_blocks;
  227.      CORE_ADDR start, end;
  228. {
  229.   register struct pending *next, *next1;
  230.   register struct block *block;
  231.   register struct pending_block *pblock;
  232.   struct pending_block *opblock;
  233.   register int i;
  234.  
  235.   /* Count the length of the list of symbols.  */
  236.  
  237.   for (next = *listhead, i = 0; next; next = next->next, i++);
  238.  
  239.   block = (struct block *)
  240.         obstack_alloc (symbol_obstack, sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
  241.  
  242.   /* Copy the symbols into the block.  */
  243.  
  244.   BLOCK_NSYMS (block) = i;
  245.   for (next = *listhead; next; next = next->next)
  246.     BLOCK_SYM (block, --i) = next->symbol;
  247.  
  248.   BLOCK_START (block) = start;
  249.   BLOCK_END (block) = end;
  250.   BLOCK_SUPERBLOCK (block) = 0;    /* Filled in when containing block is made */
  251.  
  252.   /* Put the block in as the value of the symbol that names it.  */
  253.  
  254.   if (symbol)
  255.     {
  256.       SYMBOL_BLOCK_VALUE (symbol) = block;
  257.       BLOCK_FUNCTION (block) = symbol;
  258.     }
  259.   else
  260.     BLOCK_FUNCTION (block) = 0;
  261.  
  262.   /* Now free the links of the list, and empty the list.  */
  263.  
  264.   for (next = *listhead; next; next = next1)
  265.     {
  266.       next1 = next->next;
  267.       free (next);
  268.     }
  269.   *listhead = 0;
  270.  
  271.   /* Install this block as the superblock
  272.      of all blocks made since the start of this scope
  273.      that don't have superblocks yet.  */
  274.  
  275.   opblock = 0;
  276.   for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
  277.     {
  278.       if (BLOCK_SUPERBLOCK (pblock->block) == 0)
  279.     BLOCK_SUPERBLOCK (pblock->block) = block;
  280.       opblock = pblock;
  281.     }
  282.  
  283.   /* Record this block on the list of all blocks in the file.
  284.      Put it after opblock, or at the beginning if opblock is 0.
  285.      This puts the block in the list after all its subblocks.  */
  286.  
  287.   pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
  288.   pblock->block = block;
  289.   if (opblock)
  290.     {
  291.       pblock->next = opblock->next;
  292.       opblock->next = pblock;
  293.     }
  294.   else
  295.     {
  296.       pblock->next = pending_blocks;
  297.       pending_blocks = pblock;
  298.     }
  299. }
  300.  
  301. static struct blockvector *
  302. make_blockvector ()
  303. {
  304.   register struct pending_block *next, *next1;
  305.   register struct blockvector *blockvector;
  306.   register int i;
  307.  
  308.   /* Count the length of the list of blocks.  */
  309.  
  310.   for (next = pending_blocks, i = 0; next; next = next->next, i++);
  311.  
  312.   blockvector = (struct blockvector *)
  313.           obstack_alloc (symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
  314.  
  315.   /* Copy the blocks into the blockvector.
  316.      This is done in reverse order, which happens to put
  317.      the blocks into the proper order (ascending starting address).
  318.      finish_block has hair to insert each block into the list
  319.      after its subblocks in order to make sure this is true.  */
  320.  
  321.   BLOCKVECTOR_NBLOCKS (blockvector) = i;
  322.   for (next = pending_blocks; next; next = next->next)
  323.     BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
  324.  
  325.   /* Now free the links of the list, and empty the list.  */
  326.  
  327.   for (next = pending_blocks; next; next = next1)
  328.     {
  329.       next1 = next->next;
  330.       free (next);
  331.     }
  332.   pending_blocks = 0;
  333.  
  334.   return blockvector;
  335. }
  336.  
  337. /* Manage the vector of line numbers.  */
  338.  
  339. static
  340. record_line (line, pc)
  341.      int line;
  342.      CORE_ADDR pc;
  343. {
  344. }
  345.  
  346. /* Start a new symtab for a new source file.
  347.    This is called when a COFF ".file" symbol is seen;
  348.    it indicates the start of data for one original source file.  */
  349.  
  350. static void
  351. start_symtab ()
  352. {
  353.   context_stack = 0;
  354.   within_function = 0;
  355.   last_source_file = 0;
  356.  
  357.   /* Initialize the source file information for this file.  */
  358.  
  359. }
  360.  
  361. /* Finish the symbol definitions for one main source file,
  362.    close off all the lexical contexts for that file
  363.    (creating struct block's for them), then make the
  364.    struct symtab for that file and put it in the list of all such.
  365.  
  366.    END_ADDR is the address of the end of the file's text.  */
  367.  
  368. static void
  369. end_symtab (start_addr, end_addr, linetable)
  370.      CORE_ADDR start_addr, end_addr;
  371.      struct linetable *linetable;
  372. {
  373.   register struct symtab *symtab;
  374.   register struct context_stack *cstk;
  375.   register struct blockvector *blockvector;
  376.   struct partial_symtab *psymtab;
  377.  
  378.   if (aout_hdr.entry < end_addr
  379.       && aout_hdr.entry >= start_addr)
  380.     {
  381.       startup_file_start = start_addr;
  382.       startup_file_end = end_addr;
  383.     }
  384.  
  385.   /* Finish the lexical context of the last function in the file.  */
  386.  
  387.   if (context_stack)
  388.     {
  389.       cstk = context_stack;
  390.       context_stack = 0;
  391.       /* Make a block for the local symbols within.  */
  392.       finish_block (cstk->name, &local_symbols, cstk->old_blocks,
  393.             cstk->start_addr, end_addr);
  394.       free (cstk);
  395.     }
  396.  
  397.   /* Create the two top-level blocks for this file.  */
  398.   finish_block (0, &file_symbols, 0, start_addr, end_addr);
  399.   finish_block (0, &global_symbols, 0, start_addr, end_addr);
  400.  
  401.   /* Create the blockvector that points to all the file's blocks.  */
  402.   blockvector = make_blockvector ();
  403.  
  404.   /* Now create the symtab object for this source file.  */
  405.   symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
  406.   symtab->free_ptr = 0;
  407.  
  408.   /* Fill in its components.  */
  409.   symtab->blockvector = blockvector;
  410.   symtab->free_code = free_linetable;
  411.   symtab->filename = last_source_file;
  412.   symtab->linetable = linetable;
  413.   symtab->nlines = 0;
  414.   symtab->line_charpos = 0;
  415.  
  416.   /* Link the new symtab into the list of such.  */
  417.   symtab->next = symtab_list;
  418.   symtab_list = symtab;
  419.  
  420.   /* Reinitialize for beginning of new file. */
  421.   last_source_file = 0;
  422.  
  423.   /* Create a fake partial_symtab, so that find_pc_partial_function
  424.    * will do the right thing. */
  425.   psymtab = (struct partial_symtab *)
  426.       obstack_alloc (psymbol_obstack,
  427.              sizeof (struct partial_symtab));
  428.   bzero (psymtab, sizeof (struct partial_symtab));
  429.   psymtab->next = partial_symtab_list;
  430.   partial_symtab_list = psymtab;
  431.   psymtab->textlow = start_addr;
  432.   psymtab->texthigh = end_addr;
  433.   psymtab->filename = symtab->filename ? symtab->filename : "";
  434.   psymtab->readin = 1;
  435.  
  436. }
  437.  
  438. /* Accumulate the misc functions in bunches of 127.
  439.    At the end, copy them all into one newly allocated structure.  */
  440.  
  441. #define MISC_BUNCH_SIZE 127
  442.  
  443. struct misc_bunch
  444. {
  445.   struct misc_bunch *next;
  446.   struct misc_function contents[MISC_BUNCH_SIZE];
  447. };
  448.  
  449. /* Bunch currently being filled up.
  450.    The next field points to chain of filled bunches.  */
  451.  
  452. static struct misc_bunch *misc_bunch;
  453.  
  454. /* Number of slots filled in current bunch.  */
  455.  
  456. static int misc_bunch_index;
  457.  
  458. /* Total number of misc functions recorded so far.  */
  459.  
  460. static int misc_count;
  461.  
  462. static void
  463. init_misc_functions ()
  464. {
  465.   misc_count = 0;
  466.   misc_bunch = 0;
  467.   misc_bunch_index = MISC_BUNCH_SIZE;
  468. }
  469.  
  470. static void
  471. record_misc_function (name, address)
  472.      char *name;
  473.      CORE_ADDR address;
  474. {
  475.   register struct misc_bunch *new;
  476.  
  477.   if (misc_bunch_index == MISC_BUNCH_SIZE)
  478.     {
  479.       new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch));
  480.       misc_bunch_index = 0;
  481.       new->next = misc_bunch;
  482.       misc_bunch = new;
  483.     }
  484.   misc_bunch->contents[misc_bunch_index].name = savestring (name, strlen (name));
  485.   misc_bunch->contents[misc_bunch_index].address = address;
  486.   misc_bunch->contents[misc_bunch_index].type = (char)mf_unknown;
  487.   misc_bunch_index++;
  488.   misc_count++;
  489. }
  490.  
  491. /* if we see a function symbol, we do record_misc_function.
  492.  * however, if it turns out the next symbol is '.bf', then
  493.  * we call here to undo the misc definition
  494.  */
  495. static void
  496. unrecord_misc_function ()
  497. {
  498.   if (misc_bunch_index == 0)
  499.     error ("Internal error processing symbol table, at symbol %d.",
  500.        symnum);
  501.   misc_bunch_index--;
  502.   misc_count--;
  503. }
  504.  
  505.  
  506. static int
  507. compare_misc_functions (fn1, fn2)
  508.      struct misc_function *fn1, *fn2;
  509. {
  510.   /* Return a signed result based on unsigned comparisons
  511.      so that we sort into unsigned numeric order.  */
  512.   if (fn1->address < fn2->address)
  513.     return -1;
  514.   if (fn1->address > fn2->address)
  515.     return 1;
  516.   return 0;
  517. }
  518.  
  519. static void
  520. discard_misc_bunches ()
  521. {
  522.   register struct misc_bunch *next;
  523.  
  524.   while (misc_bunch)
  525.     {
  526.       next = misc_bunch->next;
  527.       free (misc_bunch);
  528.       misc_bunch = next;
  529.     }
  530. }
  531.  
  532. static void
  533. condense_misc_bunches ()
  534. {
  535.   register int i, j;
  536.   register struct misc_bunch *bunch;
  537. #ifdef NAMES_HAVE_UNDERSCORE
  538.   int offset = 1;
  539. #else
  540.   int offset = 0;
  541. #endif
  542.  
  543.   misc_function_vector
  544.     = (struct misc_function *)
  545.       xmalloc (misc_count * sizeof (struct misc_function));
  546.  
  547.   j = 0;
  548.   bunch = misc_bunch;
  549.   while (bunch)
  550.     {
  551.       for (i = 0; i < misc_bunch_index; i++)
  552.     {
  553.       register char *tmp;
  554.  
  555.       misc_function_vector[j] = bunch->contents[i];
  556.       tmp = misc_function_vector[j].name;
  557.       misc_function_vector[j].name = (tmp[0] == '_' ? tmp + offset : tmp);
  558.       j++;
  559.     }
  560.       bunch = bunch->next;
  561.       misc_bunch_index = MISC_BUNCH_SIZE;
  562.     }
  563.  
  564.   misc_function_count = j;
  565.  
  566.   /* Sort the misc functions by address.  */
  567.  
  568.   qsort (misc_function_vector, j, sizeof (struct misc_function),
  569.      compare_misc_functions);
  570. }
  571.  
  572. /* Call sort_syms to sort alphabetically
  573.    the symbols of each block of each symtab.  */
  574.  
  575. static int
  576. compare_symbols (s1, s2)
  577.      struct symbol **s1, **s2;
  578. {
  579.   /* Names that are less should come first.  */
  580.   register int namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
  581.   if (namediff != 0) return namediff;
  582.   /* For symbols of the same name, registers should come first.  */
  583.   return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
  584.       - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
  585. }
  586.  
  587. static void
  588. sort_syms ()
  589. {
  590.   register struct symtab *s;
  591.   register int i, nbl;
  592.   register struct blockvector *bv;
  593.   register struct block *b;
  594.  
  595.   for (s = symtab_list; s; s = s->next)
  596.     {
  597.       bv = BLOCKVECTOR (s);
  598.       nbl = BLOCKVECTOR_NBLOCKS (bv);
  599.       for (i = 0; i < nbl; i++)
  600.     {
  601.       b = BLOCKVECTOR_BLOCK (bv, i);
  602.       if (BLOCK_SHOULD_SORT (b))
  603.           qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
  604.              sizeof (struct symbol *), compare_symbols);
  605.     }
  606.     }
  607. }
  608.  
  609. /* Call sort_proc_descs to sort the procedure descriptors by address.  */
  610.  
  611. static int
  612. compare_proc_descs (p1, p2)
  613. PDR *p1, *p2;
  614. {
  615.   unsigned int a1, a2;
  616.  
  617.   a1 = PROC_LOW_ADDR (p1);
  618.   a2 = PROC_LOW_ADDR (p2);
  619.   if (a1 < a2)
  620.     return -1;
  621.   if (a1 > a2)
  622.     return 1;
  623.   return 0;
  624. }
  625.  
  626. static void
  627. sort_proc_descs ()
  628. {
  629.   qsort (proc_desc_table, proc_desc_length,
  630.      sizeof (PDR), compare_proc_descs);
  631. }
  632.  
  633.  
  634. /* This is the symbol-file command.  Read the file, analyze its symbols,
  635.    and add a struct symtab to symtab_list.  */
  636.  
  637. /* !!! !!! */
  638. int dump_stuff=1;
  639.  
  640. static void free_proc_descs ()
  641. {
  642.     if (proc_desc_table != NULL) {
  643.     free (proc_desc_table);
  644.     proc_desc_table = NULL;
  645.     proc_desc_length = 0;
  646.     }
  647. }
  648.  
  649. void
  650. symbol_file_command (name)
  651.      char *name;
  652. {
  653.   int desc;
  654.   int num_symbols;
  655.   int num_sections;
  656.   int symtab_offset;
  657.   register int val;
  658.   struct cleanup *old_chain;
  659.  
  660.   dont_repeat ();
  661.  
  662.   if (name == 0)
  663.     {
  664.       if (symtab_list && !query ("Discard symbol table? ", 0))
  665.     error ("Not confirmed.");
  666.       if (symfile)
  667.     free (symfile);
  668.       symfile = 0;
  669.       free_all_symtabs ();
  670.       free_proc_descs ();
  671.       return;
  672.     }
  673.  
  674.   name = tilde_expand (name);
  675.   make_cleanup (free, name);
  676.  
  677.   if (symtab_list && !query ("Load new symbol table from \"%s\"? ", name))
  678.     error ("Not confirmed.");
  679.  
  680.   if (symfile)
  681.     free (symfile);
  682.   symfile = 0;
  683.  
  684.   {
  685.     char *absolute_name;
  686.  
  687.     desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
  688.     if (desc < 0)
  689.       perror_with_name (name);
  690.     else
  691.       name = absolute_name;
  692.   }
  693.  
  694.   old_chain = make_cleanup (close, desc);
  695.   make_cleanup (free_current_contents, &name);
  696.  
  697.   if ((num_symbols = read_file_hdr (desc, &file_hdr)) < 0)
  698.     error ("File \"%s\" not in executable format.", name);
  699.  
  700.   /* If an a.out header is present, read it in.  If not (e.g. a .o file)
  701.      deal with its absence.  */
  702.   if (file_hdr.f_opthdr == 0
  703.       || read_aout_hdr (desc, &aout_hdr, file_hdr.f_opthdr) < 0)
  704.     {
  705.       /* We will not actually be able to run code, since backtraces would
  706.      fly off the bottom of the stack (there is no way to reliably
  707.      detect bottom of stack), but that's fine since the kernel won't
  708.      run something without an a.out header anyway.  Passive examination
  709.      of .o files is one place this might make sense.  */
  710.       /* ~0 will not be in any file.  */
  711.       aout_hdr.entry = ~0;
  712.       /* set the startup file to be an empty range.  */
  713.       startup_file_start = 0;
  714.       startup_file_end = 0;
  715.     }
  716.  
  717.   if (num_symbols == 0)
  718.     {
  719.       free_all_symtabs ();
  720.       free_proc_descs ();
  721.       printf ("%s does not have a symbol-table.\n", name);
  722.       fflush (stdout);
  723.       do_cleanups (old_chain);
  724.       return;
  725.     }
  726.  
  727.   printf ("Reading symbol data from %s...", name);
  728.   fflush (stdout);
  729.  
  730.   /* Throw away the old symbol table.  */
  731.  
  732.   free_all_symtabs ();
  733.   free_proc_descs ();
  734.   free_all_psymtabs ();        /* Make sure that partial_symtab_list */
  735.                 /* is 0 also. */
  736.  
  737.   num_sections = file_hdr.f_nscns;
  738.   symtab_offset = file_hdr.f_symptr;
  739.  
  740.   if (read_section_hdr (desc, _TEXT, &text_hdr, num_sections) < 0)
  741.     error ("\"%s\": can't read text section header", name);
  742.  
  743.   /* Position to read the symbol table.  Do not read it all at once. */
  744.   val = lseek (desc, (long)symtab_offset, 0);
  745.   if (val < 0)
  746.     perror_with_name (name);
  747.  
  748.   init_misc_functions ();
  749.   make_cleanup (discard_misc_bunches, 0);
  750.  
  751.   /* Now that the executable file is positioned at symbol table,
  752.      process it and define symbols accordingly.  */
  753.  
  754.   val = read_coff_symtab (desc, num_symbols, symtab_offset);
  755.   if (val < 0) error("Bad symbol table format");
  756.  
  757.   patch_opaque_types ();
  758.  
  759.   /* Sort symbols alphabetically within each block.  */
  760.  
  761.   sort_syms ();
  762.  
  763.   /* Go over the misc functions and install them in vector.  */
  764.  
  765.   condense_misc_bunches ();
  766.  
  767.   /* Sort the procedure descriptor table.  */
  768.  
  769.   sort_proc_descs ();
  770.  
  771.   /* Don't allow char * to have a typename (else would get caddr_t.)  */
  772.  
  773.   TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
  774.  
  775.   /* Make a default for file to list.  */
  776.  
  777.   symfile = savestring (name, strlen (name));
  778.  
  779.   do_cleanups (old_chain);
  780.  
  781.   printf ("done.\n");
  782.   fflush (stdout);
  783. }
  784.  
  785. /* Return name of file symbols were loaded from, or 0 if none..  */
  786.  
  787. char *
  788. get_sym_file ()
  789. {
  790.   return symfile;
  791. }
  792.  
  793. /* Simplified internal version of coff symbol table information */
  794.  
  795. struct coff_symbol {
  796.   char *c_name;
  797.   int c_symnum;        /* symbol number of this entry */
  798.   int c_nsyms;        /* 1 if syment only, 2 if syment + auxent */
  799.   long c_value;
  800.   int c_sclass;
  801.   int c_secnum;
  802.   unsigned int c_type;
  803. };
  804.  
  805. static char *
  806. read_table(desc, size, offset)
  807.     long offset;
  808. {
  809.     char *buf;
  810.     if (lseek(desc, offset, 0) < 0)
  811.       error("Bad symbol table format [seek]");
  812.     buf = xmalloc(size);
  813.     if (buf == NULL) error("Not enough memory");
  814.     if (myread (desc, buf, size) != size)
  815.       error("Bad symbol table format [read]");
  816.     return buf;
  817. }
  818.  
  819. static char * (MapStNames[]) = { /* symbol type names */
  820.     "Nil", "Global", "Static", "Param", "Local", "Label", "Proc", "Block",
  821.     "End", "Member", "Typedef", "File", "RegReloc", "Forward", "StaticProc",
  822.     "Constant", "BlockPatched"
  823. };
  824.  
  825. long stNameSize = sizeof(MapStNames)/sizeof(MapStNames[0]);
  826.  
  827. static char * MapScNames[] = {    /* storage class names */
  828.     "Nil", "Text", "Data", "Bss", "Register", "Abs", "Undefined", "CdbLocal",
  829.     "Bits", "Dbx", "RegImage", "Info", "UserStruct", "SData", "SBss", "RData",
  830.     "Var", "Common", "SCommon", "VarRegister", "Variant", "SUndefined", "Init"
  831. };
  832.  
  833.  
  834. long scNameSize = sizeof(MapScNames)/sizeof(MapScNames[0]);
  835.  
  836. static char *(MapBtNames[]) = {    /* base type names */
  837.     "Nil", "Adr", "Char", "UChar", "Short", "UShort", "Int", "UInt", "Long",
  838.     "ULong", "Float", "Double", "Struct", "Union", "Enum", "Typedef", "Range",
  839.     "Set", "Complex", "DComplex", "Indirect", "FixedDec", "FloatDec", "String",
  840.     "Bit", "Picture"
  841. };
  842.  
  843. long btNameSize = sizeof(MapBtNames)/sizeof(MapBtNames[0]);
  844.  
  845. /* Given pointers to a symbol table in coff style exec file,
  846.    analyze them and create struct symtab's describing the symbols.
  847.    NSYMS is the number of symbols in the symbol table.
  848.    We read them one at a time using read_one_sym ().  */
  849.  
  850. /* stBlockPatched indicates an stBlock symbol which has been patched
  851.  * so that the value points to s struct type */
  852.  
  853. #define stBlockPatched 16
  854.  
  855. static FDR *file_descriptor_table;
  856. static RFDT *rel_file_table;
  857. static SYMR *local_symbol_table;
  858. static char *local_string_table;
  859. static AUXU *aux_symbol_table;
  860. static FDR *cur_file_descriptor;
  861. static int file_descriptor_count;
  862. /* Note that PDR is also used for frame chaining, and is defined above */
  863.  
  864. static int cur_proc_number; /* number of procedure within current file */
  865. static CORE_ADDR cur_proc_addr;
  866. static int cur_file_number;
  867. static int cur_isymBase;
  868. static int cur_issBase;
  869.  
  870. static void
  871. select_file(i)
  872.     int i;
  873. {
  874.     global_symbols_all[cur_file_number] = global_symbols;
  875.     file_symbols_all[cur_file_number] = file_symbols;
  876.     global_symbols = global_symbols_all[i];
  877.     file_symbols = file_symbols_all[i];
  878.     cur_file_number = i;
  879.     cur_file_descriptor = &file_descriptor_table[i];
  880.     cur_isymBase = cur_file_descriptor->isymBase;
  881.     cur_issBase = cur_file_descriptor->issBase;
  882. }
  883.  
  884. /* side-effect: changes cur_file_descriptor */
  885.  
  886. static SYMR *
  887. get_type_context(auxp)
  888.     AUXU** auxp;
  889. {
  890.   SYMR *sym;
  891.   FDR *sym_file_desc;
  892.   int rfi;
  893.   int rfd = (*auxp)->rndx.rfd;
  894.   int sym_index = (*auxp)->rndx.index;
  895.   (*auxp)++;
  896.   if (rfd == ST_RFDESCAPE) { rfd = (*auxp)->isym; (*auxp)++; }
  897.   if (rfd == ST_EXTIFD || sym_index == ST_ANONINDEX) return NULL;
  898.   rfi = rel_file_table[cur_file_descriptor->rfdBase + rfd];
  899.   if (rfi >= file_descriptor_count) return NULL;
  900.   sym_file_desc = &file_descriptor_table[rfi];
  901.   if (sym_index >= sym_file_desc->csym || sym_index == 0) return NULL;
  902.   sym = &local_symbol_table[sym_file_desc->isymBase + sym_index];
  903.   if (sym->index == 0) return NULL;
  904.   select_file(rfi);
  905.   if (dump_stuff)
  906.     printf("rfd(%d,%d)->[%s,%x,st%s,sc%s,inx:%d (%x)]\n",
  907.      rfi, sym_index,
  908.      &local_string_table[cur_issBase+sym->iss],
  909.      sym->value,
  910.            MapStNames[sym->st], MapScNames[sym->sc],
  911.            sym->index, sym);
  912.   return sym;
  913. }
  914.  
  915. static struct type *
  916. get_struct_type(sym, kind)
  917.      SYMR *sym;
  918.      int kind; /* either btStruct or btUnion or btNil (unknown) */
  919. {
  920.   struct type *type;
  921.   if (sym->st == stBlockPatched)
  922.     {
  923.       type = (struct type*)sym->value;
  924.       /* Patch up possibly-wrong guess about struct vs. union */
  925.       if (kind == btStruct) TYPE_CODE(type) = TYPE_CODE_STRUCT;
  926.       else if (kind == btUnion) TYPE_CODE(type) = TYPE_CODE_UNION;
  927.     }
  928.   else if (sym->st != stBlock)
  929.     {
  930.       fprintf(stderr,"Bad symbol table: struct/union points to non-stBlock\n");
  931.       type = builtin_type_void;
  932.     }
  933.   else
  934.     {
  935.       int nfields = 0;
  936.       SYMR *first_field = sym+1;
  937.       register struct field *cur_field;
  938.       char *sym_name = &local_string_table[cur_issBase+sym->iss];
  939.       int iauxBase = cur_file_descriptor->iauxBase;
  940.       type = (struct type *) obstack_alloc (symbol_obstack,
  941.                         sizeof (struct type));
  942.       bzero (type, sizeof (struct type));
  943.       TYPE_LENGTH (type) = sym->value;
  944.  
  945.       /* we "remember" the type generated from this block */
  946.       sym->st = stBlockPatched;
  947.       sym->value = (long)type;
  948.  
  949.       /* first count the number of fields */
  950.       for (sym = first_field; sym->st != stEnd; sym++)
  951.     if (sym->st == stMember) nfields++;
  952.     else if (sym->st == stBlock || sym->st == stBlockPatched)
  953.       {
  954.         if (sym->sc == scVariant) ; /* UNIMPLEMENTED */
  955.         if (sym->index != 0)
  956.           sym = &local_symbol_table[cur_isymBase + sym->index - 1];
  957.       }
  958.  
  959.       TYPE_NFIELDS (type) = nfields;
  960.       TYPE_FIELDS (type) = cur_field = (struct field*)
  961.     obstack_alloc (symbol_obstack, nfields * sizeof (struct field));
  962.       for (sym = first_field; sym->st != stEnd; sym++)
  963.     if (sym->st == stMember)
  964.       {
  965.         AUXU *aux = &aux_symbol_table[iauxBase + sym->index];
  966.         char *name = &local_string_table[cur_issBase+sym->iss];
  967.         cur_field->name =
  968.           obstack_copy0 (symbol_obstack, name, strlen (name));
  969.  
  970.         cur_field->type = read_type(sym->index, &cur_field->bitsize);
  971.         cur_field->bitpos = sym->value;
  972.         cur_field++;
  973.       }
  974.     else if (sym->st == stTypedef) ; /* just ignore it */
  975.     else if (sym->st == stBlock || sym->st == stBlockPatched)
  976.       {
  977.         if (sym->sc == scVariant) ; /* UNIMPLEMENTED */
  978.         if (sym->index != 0)
  979.           sym = &local_symbol_table[cur_isymBase + sym->index - 1];
  980.       }
  981.     else
  982.       fprintf(stderr, "[Bad member in struct/union field list]\n");
  983.       /*
  984.        * Heuristic: if 2nd field has offset 0, this is a union,
  985.        * otherwise, a struct definition.
  986.        * If nfields <= 1, we guess struct.
  987.        * If the type is used later, we fix it up.
  988.        * (This is done in the stBlockPatched case at the top of this routine).
  989.        */
  990.       if (kind == btNil && nfields > 1)
  991.     {
  992.       if (TYPE_FIELDS(type)[1].bitpos > 0) kind = btStruct;
  993.       else kind = btUnion;
  994.     }
  995.       TYPE_CODE (type) = kind == btUnion ? TYPE_CODE_UNION : TYPE_CODE_STRUCT;
  996.       TYPE_NAME (type) = concat ("",
  997.                  (kind == btUnion ? "union " : "struct "),
  998.                  sym_name);
  999.     }
  1000.     return type;
  1001. }
  1002.  
  1003. static struct type *
  1004. read_struct_type(auxp, kind)
  1005.      AUXU** auxp;
  1006.      int kind; /* either btStruct or btUnion */
  1007. {
  1008.   struct type *type;
  1009.   int save_file_number = cur_file_number;
  1010.   SYMR *sym = get_type_context(auxp);
  1011.   if (sym == NULL) {
  1012.     /*
  1013.      * If we get here then there is reference to a structure that is never
  1014.      * defined.  Such a thing is perfectly legal (e.g. a pointer to a
  1015.      * structure that is never defined), and the error message is annoying.
  1016.      */
  1017. #ifndef sprite
  1018.     printf(stderr, "Bad symbol for struct/union definition\n");
  1019. #endif
  1020.     return builtin_type_void;
  1021.   }
  1022.   if (sym->st == stTypedef)
  1023.       type = read_type(sym->index, NULL);
  1024.   else
  1025.       type = get_struct_type(sym, kind);
  1026.   select_file (save_file_number);
  1027.   return type;
  1028. }
  1029.  
  1030. static struct type *
  1031. get_enum_type(sym)
  1032.     SYMR *sym;
  1033. {
  1034.   struct type *type;
  1035.   if (sym->st == stBlockPatched)
  1036.     type = (struct type*)sym->value;
  1037.   else if (sym->st != stBlock)
  1038.     {
  1039.       fprintf(stderr,"Bad symbol table: enum points to non-stBlock\n");
  1040.       type = builtin_type_void;
  1041.     }
  1042.   else
  1043.     {
  1044.       int nfields = 0;
  1045.       SYMR *first_field = sym+1;
  1046.       register struct field *cur_field;
  1047.       type = (struct type *) obstack_alloc (symbol_obstack,
  1048.                         sizeof (struct type));
  1049.       bzero (type, sizeof (struct type));
  1050.       TYPE_LENGTH (type) = sym->value;
  1051.  
  1052.       /* we "remember" the type generated from this block */
  1053.       sym->st = stBlockPatched;
  1054.       sym->value = (long)type;
  1055.  
  1056.       TYPE_CODE (type) = TYPE_CODE_ENUM;
  1057.       TYPE_LENGTH (type) = sizeof (int);
  1058.       TYPE_NAME (type) = concat ("", "enum ",
  1059.                  &local_string_table[cur_issBase+sym->iss]);
  1060.  
  1061.       nfields = &local_symbol_table[cur_isymBase+sym->index] - first_field - 1;
  1062.  
  1063.       TYPE_NFIELDS (type) = nfields;
  1064.       TYPE_FIELDS (type) = cur_field = (struct field*)
  1065.     obstack_alloc (symbol_obstack, nfields * sizeof (struct field));
  1066.       for (sym = first_field; sym->st != stEnd; sym++)
  1067.     if (sym->st == stMember)
  1068.       {
  1069.         char *name = &local_string_table[cur_issBase+sym->iss];
  1070.         cur_field->name =
  1071.           obstack_copy0 (symbol_obstack, name, strlen (name));
  1072.  
  1073.         cur_field->bitpos = sym->value;
  1074.         cur_field->bitsize = 0;
  1075.         cur_field++;
  1076.       }
  1077.     else
  1078.       fprintf(stderr, "[Bad member in enum list]\n");
  1079.     }
  1080.   return type;
  1081. }
  1082.  
  1083. static struct type *
  1084. read_enum_type(auxp)
  1085.      AUXU** auxp;
  1086. {
  1087.   struct type *type;
  1088.   int save_file_number = cur_file_number;
  1089.   SYMR *sym = get_type_context(auxp);
  1090.   if (sym == NULL) {
  1091.     fprintf(stderr, "Bad symbol for enum definition");
  1092.     return builtin_type_int;
  1093.   }
  1094.   if (sym->st == stTypedef)
  1095.       type = read_type(sym->index, NULL);
  1096.   else
  1097.       type = get_enum_type (sym);
  1098.   select_file (save_file_number);
  1099.   return type;
  1100. }
  1101.  
  1102. static struct type *
  1103. read_range_type(auxp)
  1104.      AUXU** auxp;
  1105. {
  1106.   struct type *range_type;
  1107.   int save_file_number = cur_file_number;
  1108.   SYMR *sym = get_type_context(auxp);
  1109.   /* sym (if non-NULL) may point at an stBlock/scInfo, but ignore it */
  1110.    range_type = (struct type *) obstack_alloc (symbol_obstack,
  1111.                           sizeof (struct type));
  1112.   TYPE_CODE (range_type) = TYPE_CODE_RANGE;
  1113.   TYPE_TARGET_TYPE (range_type) = builtin_type_int;
  1114.   TYPE_LENGTH (range_type) = sizeof (int);
  1115.   TYPE_NFIELDS (range_type) = 2;
  1116.   TYPE_FIELDS (range_type) =
  1117.       (struct field *) obstack_alloc (symbol_obstack,
  1118.                       2 * sizeof (struct field));
  1119.   TYPE_FIELD_BITPOS (range_type, 0) = (*auxp)++->dnLow;
  1120.   TYPE_FIELD_BITPOS (range_type, 1) = (*auxp)++->dnHigh;
  1121.  
  1122.   select_file (save_file_number);
  1123.   return range_type;
  1124. }
  1125.  
  1126. static struct type *
  1127. modify_type(type, qualifier, auxp)
  1128.      struct type *type;
  1129.      int qualifier;
  1130.      AUXU** auxp;
  1131. {
  1132.     switch (qualifier) {
  1133.       case tqPtr: return lookup_pointer_type(type);
  1134.       case tqNil: return type;
  1135.       case tqArray:
  1136.     {
  1137.         struct type *range_type = read_range_type(auxp);
  1138.         long lower = TYPE_FIELD_BITPOS (range_type, 0);
  1139.         long upper = TYPE_FIELD_BITPOS (range_type, 1);
  1140.         int elem_size = (*auxp)++->width; /* not used */
  1141.         struct type *atype;
  1142.  
  1143.         atype = (struct type *)
  1144.         obstack_alloc (symbol_obstack, sizeof (struct type));
  1145.         bzero (atype, sizeof (struct type));
  1146.  
  1147.         TYPE_CODE (atype) = TYPE_CODE_ARRAY;
  1148.         TYPE_TARGET_TYPE (atype) = type;
  1149.         TYPE_LENGTH (atype) = (upper - lower + 1) * TYPE_LENGTH (type);
  1150.         TYPE_NFIELDS (atype) = 1;
  1151.         TYPE_FIELDS (atype) =
  1152.         (struct field *) obstack_alloc (symbol_obstack,
  1153.                         sizeof (struct field));
  1154.         TYPE_FIELD_TYPE (atype, 0) = range_type;
  1155.         return atype;
  1156.     }
  1157.       case tqVol: return type;
  1158.       case tqProc: return lookup_function_type(type);
  1159.       default:
  1160.     fprintf(stderr, "[Unimplemented type qualifier: %d]\n", qualifier);
  1161.     return type;
  1162.     }
  1163. }
  1164.  
  1165. static struct type *
  1166. apply_type_modifiers(type, tip, auxp)
  1167.      struct type *type;
  1168.      TIR *tip;
  1169.      AUXU** auxp;
  1170. {
  1171.     for (;;) {   
  1172.     if (tip->tq0 == tqNil) return type;
  1173.     type = modify_type(type, tip->tq0, auxp);
  1174.     if (tip->tq1 == tqNil) return type;
  1175.     type = modify_type(type, tip->tq1, auxp);
  1176.     if (tip->tq2 == tqNil) return type;
  1177.     type = modify_type(type, tip->tq2, auxp);
  1178.     if (tip->tq3 == tqNil) return type;
  1179.     type = modify_type(type, tip->tq3, auxp);
  1180.     if (tip->tq4 == tqNil) return type;
  1181.     type = modify_type(type, tip->tq4, auxp);
  1182.     if (tip->tq5 == tqNil) return type;
  1183.     type = modify_type(type, tip->tq5, auxp);
  1184.     if (!tip->continued) return type;
  1185.     tip++;
  1186.     }
  1187. }
  1188.  
  1189. static struct type *
  1190. read_type(index, widthp)
  1191.      int index;
  1192.      int *widthp; /* if non-NULL: set to (if bit field: width, else: 0) */
  1193. {
  1194.     TIR *tip;
  1195.     AUXU *aux;
  1196.     struct type *base_type;
  1197.     int width;
  1198.     if (index == 0xfffff) return builtin_type_int; /* no type info */
  1199. /*    if (index < 0 || index > ) ...; */
  1200.  
  1201.     aux = &aux_symbol_table[cur_file_descriptor->iauxBase + index];
  1202.     tip = &aux->ti;
  1203.     for ( ; aux->ti.continued; aux++) ;
  1204.     aux++; /* skip last TIR field */
  1205.  
  1206.     /* sym.h claims that width comes after RNDX, but seems to be wrong */
  1207.     width = tip->fBitfield ? (aux++)->width : 0;
  1208.     if (widthp) *widthp = width;
  1209.  
  1210.     switch (tip->bt) {
  1211.       case btNil: base_type = builtin_type_void; break;
  1212.       case btAdr: base_type = lookup_pointer_type(builtin_type_void); break;
  1213.       case btChar: base_type = builtin_type_char; break;
  1214.       case btUChar: base_type = builtin_type_unsigned_char; break;
  1215.       case btShort: base_type = builtin_type_short; break;
  1216.       case btUShort: base_type = builtin_type_unsigned_short; break;
  1217.       case btInt: base_type = builtin_type_int; break;
  1218.       case btUInt: base_type = builtin_type_unsigned_int; break;
  1219.       case btLong: base_type = builtin_type_long; break;
  1220.       case btULong: base_type = builtin_type_unsigned_long; break;
  1221.       case btFloat: base_type = builtin_type_float; break;
  1222.       case btDouble: base_type = builtin_type_double; break;
  1223.       case btStruct: case btUnion:
  1224.     base_type = read_struct_type(&aux, tip->bt); break;
  1225.       case btEnum:
  1226.     base_type = read_enum_type(&aux); break;
  1227.       case btRange:
  1228.     base_type = read_range_type(&aux); break;
  1229.       case btTypedef:
  1230.       case btSet: case btComplex: case btDComplex:
  1231.       case btIndirect:
  1232.       case btFixedDec: case btFloatDec: case btString:
  1233.       case btBit: case btPicture:
  1234.       default:
  1235.         /*
  1236.      * Don't complain about void types (26). 
  1237.      */
  1238.         if (tip->bt != 26) {
  1239.         fprintf(stderr, "[Unimplemented kind of type: %d]\n", tip->bt);
  1240.     }
  1241.     base_type = builtin_type_void;
  1242.     }
  1243.     return apply_type_modifiers(base_type, tip, &aux);   
  1244. }
  1245.  
  1246. static struct symbol *
  1247. alloc_symbol(name, value)
  1248.   char *name;
  1249.   int value;
  1250. {
  1251.   register struct symbol *sym;
  1252.   sym = (struct symbol *)obstack_alloc (symbol_obstack, sizeof(struct symbol));
  1253. #ifdef NAMES_HAVE_UNDERSCORE
  1254.   if (name[0] == '_') name++;
  1255. #endif
  1256.  
  1257.   bzero (sym, sizeof (struct symbol));
  1258.   SYMBOL_NAME (sym) = obstack_copy0 (symbol_obstack, name, strlen (name));
  1259.  
  1260.   /* default assumptions */
  1261.   SYMBOL_VALUE (sym) = value;
  1262.   SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1263.   SYMBOL_TYPE (sym) = builtin_type_void;
  1264.  
  1265.   return sym;
  1266. }
  1267.  
  1268. static
  1269. read_symbol(csym, name)
  1270.   SYMR *csym;
  1271.   char *name;
  1272. {
  1273.   register struct symbol *sym = alloc_symbol(name, csym->value);
  1274.  
  1275.   switch (csym->st)
  1276.     {
  1277.     case stNil: break;
  1278.     case stFile:
  1279.       last_source_file = obstack_copy0 (symbol_obstack, name, strlen (name));
  1280.       break;
  1281.     case stGlobal:
  1282.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1283.       SYMBOL_CLASS (sym) = LOC_STATIC;
  1284.       add_symbol_to_list (sym, &global_symbols);
  1285.       break;
  1286.     case stStatic:
  1287.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1288.       SYMBOL_CLASS (sym) = LOC_STATIC;
  1289.       if (within_function) {
  1290.       /* Static symbol of local scope */
  1291.       add_symbol_to_list (sym, &local_symbols);
  1292.       }
  1293.       else {
  1294.       /* Static symbol at top level of file */
  1295.       add_symbol_to_list (sym, &file_symbols);
  1296.       }
  1297.       break;
  1298.  
  1299.     case stLocal:
  1300.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1301.       switch (csym->sc) {
  1302.     case scRegister: SYMBOL_CLASS (sym) = LOC_REGISTER; break;
  1303.     case scAbs:    SYMBOL_CLASS (sym) = LOC_LOCAL; break;
  1304.  
  1305.       }
  1306.       add_symbol_to_list (sym, &local_symbols);
  1307.       break;
  1308.  
  1309.     case stParam:
  1310.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1311.       switch (csym->sc) {
  1312.     case scAbs:      SYMBOL_CLASS (sym) = LOC_ARG; break;
  1313.     case scRegister:  SYMBOL_CLASS (sym) = LOC_REGPARM; break;
  1314.     case scVar:      SYMBOL_CLASS (sym) = LOC_REF_ARG; break;
  1315.     case scVarRegister: SYMBOL_CLASS (sym) = LOC_REGPARM; break; /*WRONG!*/
  1316.       }
  1317.       add_symbol_to_list (sym, &local_symbols);
  1318.       break;
  1319.  
  1320.     case stTypedef:
  1321.       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1322.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1323.       if (within_function) {
  1324.       /* Static symbol of local scope */
  1325.       add_symbol_to_list (sym, &local_symbols);
  1326.       }
  1327.       else {
  1328.       /* Static symbol at top level of file */
  1329.       add_symbol_to_list (sym, &file_symbols);
  1330.       }
  1331.       break;
  1332.     
  1333.     case stEnd:
  1334.     default: break;
  1335.     }
  1336.   return 1;
  1337. }
  1338.  
  1339. static int
  1340. read_tagged_block (index)
  1341. {
  1342.   SYMR *csym = &local_symbol_table[cur_isymBase+index];
  1343.   char *name = &local_string_table[cur_issBase+csym->iss];
  1344.   struct pending **symlist = within_function ? &local_symbols: &file_symbols;
  1345.   register struct symbol *sym;
  1346.  
  1347.   /* A kludge to decide if this is an enum definition. */
  1348.   if ((csym[1].st == stMember
  1349.        && read_type(csym[1].index, 0) == builtin_type_void)
  1350.    || (csym->st == stBlockPatched
  1351.        && TYPE_CODE((struct type*)(csym->value)) == TYPE_CODE_ENUM))
  1352.     {
  1353.       struct type *type = get_enum_type (csym);
  1354.       if (type && TYPE_CODE(type) == TYPE_CODE_ENUM)
  1355.         {
  1356.           int nfields = TYPE_NFIELDS (type);
  1357.           register int i;
  1358.           register struct field *cur_field = TYPE_FIELDS(type);
  1359.           if (name && name[0] && name[0] != '.')
  1360.         {
  1361.           sym = alloc_symbol(name, 0);
  1362.           SYMBOL_TYPE (sym) = type;
  1363.           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1364.           SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
  1365.           add_symbol_to_list (sym, symlist);
  1366.         }
  1367.           for (i = nfields; --i >= 0; cur_field++)
  1368.         {
  1369.           sym = alloc_symbol(cur_field->name, cur_field->bitpos);
  1370.           SYMBOL_CLASS (sym) = LOC_CONST;
  1371.           SYMBOL_TYPE (sym) = type;
  1372.           add_symbol_to_list (sym, symlist);
  1373.         }
  1374.           return nfields + 2;
  1375.       }
  1376.       }
  1377.   else
  1378.     {
  1379.       if (name && name[0] && name[0] != '.')
  1380.     {
  1381.       struct type *type = get_struct_type (csym, btNil);
  1382.       sym = alloc_symbol(name, 0);
  1383.       SYMBOL_TYPE (sym) = type;
  1384.       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1385.       SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
  1386.       add_symbol_to_list (sym, symlist);
  1387.     }
  1388.       return read_symbols (index + 1) + 2; 
  1389.     }
  1390. }
  1391.  
  1392. /* Read symbols until an stEnd symbol is encountered. Return count. */
  1393.  
  1394. static int
  1395. read_symbols(index)
  1396.      int index; /* relative to current file */
  1397. {
  1398.   register struct context_stack *new;
  1399.   static int blocks_seen = 0;
  1400.   int index0 = index;
  1401.   SYMR *start_symbol;
  1402.   for (;;)
  1403.       {
  1404.     SYMR *csym = &local_symbol_table[cur_isymBase+index];
  1405.     char *name = &local_string_table[cur_issBase+csym->iss];
  1406.  
  1407.     if (dump_stuff)
  1408.         printf("[%s,%x,st%s,sc%s,inx:%d (%x)]\n", name, csym->value,
  1409.            MapStNames[csym->st], MapScNames[csym->sc],
  1410.            csym->index, csym);
  1411.  
  1412.     switch (csym->st)
  1413.         {
  1414.         case stFile:
  1415.           last_source_file =
  1416.           obstack_copy0 (symbol_obstack, name, strlen (name));
  1417.           index++;
  1418.           index += read_symbols(index) + 1;
  1419.           return index - index0;
  1420.         case stBlock:
  1421.         case stBlockPatched:
  1422.           if (csym->sc == scText)
  1423.         { /* lexical block */
  1424.           if (!local_symbol_table[cur_isymBase+csym->index-1].value)
  1425.             {
  1426.               new = 0;
  1427.             }
  1428.           else
  1429.             {
  1430.               new = (struct context_stack *)
  1431.             xmalloc (sizeof (struct context_stack));
  1432.               new->next = context_stack;
  1433.               context_stack = new;
  1434.               new->locals = local_symbols;
  1435.               new->old_blocks = pending_blocks;
  1436.               new->start_addr = cur_proc_addr + csym->value;
  1437.               new->name = 0;
  1438.               local_symbols = 0;
  1439.             }
  1440.           blocks_seen++;
  1441.           start_symbol=csym;
  1442.           index++;
  1443.           index += read_symbols (index);
  1444.           csym = &local_symbol_table[cur_isymBase+index];  /* stEnd */
  1445.           index++;
  1446.  
  1447. if (csym->st != stEnd ||
  1448.    start_symbol != &local_symbol_table[cur_isymBase+csym->index])
  1449. abort();
  1450.           if (new)
  1451.             {
  1452.               if (local_symbols && context_stack->next)
  1453.             {
  1454.               /* Make a block for the local symbols within.  */
  1455.               finish_block (0, &local_symbols, new->old_blocks,
  1456.                     new->start_addr,
  1457.                     cur_proc_addr + csym->value);
  1458.             }
  1459.               local_symbols = new->locals;
  1460.               context_stack = new->next;
  1461.               free (new);
  1462.             }
  1463.           }
  1464.           else if (csym->sc == scInfo)
  1465.           index += read_tagged_block (index);
  1466.           break;
  1467.         case stProc:
  1468.         case stStaticProc:
  1469.           {
  1470.         PDR *proc = &proc_desc_table
  1471.             [cur_file_descriptor->ipdFirst+cur_proc_number++];
  1472.         CORE_ADDR saved_cur_proc_addr = cur_proc_addr;
  1473.         struct symbol *sym = alloc_symbol(name, csym->value);
  1474.         struct context_stack *saved_context_stack = context_stack;
  1475.         int save_blocks_seen = blocks_seen;
  1476.         SYMBOL_CLASS (sym) = LOC_BLOCK;
  1477.         /* Add one to csym->index, to the skip the isymMac value
  1478.          * (which is there only for st*Proc). */
  1479.         SYMBOL_TYPE (sym) = 
  1480.             lookup_function_type (read_type(csym->index+1, NULL));
  1481.         if (csym->st == stStaticProc)
  1482.             add_symbol_to_list (sym, &file_symbols);
  1483.         else
  1484.             add_symbol_to_list (sym, &global_symbols);
  1485.         cur_proc_addr = csym->value;
  1486.         within_function++;
  1487.         new = (struct context_stack *)
  1488.             xmalloc (sizeof (struct context_stack));
  1489.         new->next = 0;
  1490.         context_stack = new;
  1491.         new->locals = 0;
  1492.         new->old_blocks = pending_blocks;
  1493.         new->start_addr = csym->value;
  1494.         new->name = sym;
  1495.         if (dump_stuff)
  1496.             printf("[PROC:%s,@%x,framereg:%d,froff:%d,rmsk:%d,fmsk:%d,floff:%d,regoff:%d,iline:%d,lnLo:%d,lnHi:%d (%x)]\n",
  1497.                name, proc->adr,
  1498.            proc->framereg, proc->frameoffset, proc->regmask,
  1499.            proc->fregmask, proc->fregoffset,
  1500.                proc->regoffset,
  1501.                proc->iline,proc->lnLow,proc->lnHigh, proc);
  1502.  
  1503.         start_symbol = csym;
  1504.         index++;
  1505.         index += read_symbols(index);
  1506.  
  1507.         csym = &local_symbol_table[cur_isymBase+index];/*stEnd symbol*/
  1508.  
  1509. if (csym->st != stEnd ||
  1510.    start_symbol != &local_symbol_table[cur_isymBase+csym->index])
  1511. abort();
  1512.  
  1513.         index++;
  1514.         PROC_SYMBOL(proc) =
  1515.             blocks_seen == save_blocks_seen ? NULL : sym;
  1516.         PROC_LOW_ADDR(proc) = start_symbol->value;
  1517.         PROC_HIGH_ADDR(proc) = last_end_addr =
  1518.             start_symbol->value + csym->value;
  1519.  
  1520.         finish_block (new->name, &local_symbols, new->old_blocks,
  1521.                   start_symbol->value, last_end_addr);
  1522.         context_stack = saved_context_stack;
  1523.         cur_proc_addr = saved_cur_proc_addr;
  1524.         within_function--;
  1525.         free (new);
  1526.         break;
  1527.           }
  1528.  
  1529.         case stEnd:
  1530. #if 1
  1531.             return index - index0;
  1532. #else
  1533.           start_symbol = &local_symbol_table[cur_isymBase+csym->index];
  1534.           switch (start_symbol->st)
  1535.           {
  1536.           case stProc:
  1537.           case stStaticProc:
  1538.           case stFile:
  1539.           case stBlock:
  1540.           case stBlockPatched:
  1541.             return index - index0;
  1542.           default:
  1543.             index++;
  1544.           }
  1545.           break;
  1546. #endif
  1547.         default:
  1548.           read_symbol(csym, name);
  1549.           index++;
  1550.         }
  1551.       }
  1552. }
  1553.  
  1554. static int
  1555. read_coff_symtab (desc, nsyms, symtab_offset)
  1556.      int desc;
  1557.      int nsyms;
  1558.      int symtab_offset;
  1559. {
  1560.   HDRR hdrr;
  1561.   struct coff_symbol coff_symbol;
  1562.   register struct coff_symbol *cs = &coff_symbol;
  1563.   struct coff_symbol fcn_cs_saved;
  1564.  
  1565.   int num_object_files = 0;
  1566.   int next_file_symnum = -1;
  1567.   char *filestring;
  1568.   int fcn_first_line;
  1569.   int fcn_last_line;
  1570.   int fcn_start_addr;
  1571.   long fcn_line_ptr;
  1572.   struct cleanup *file_chain, *ext_chain, *old_chain;
  1573.   int isym, ifile;
  1574.  
  1575.   char *line_number_table;
  1576.   EXTR *external_symbol_table;
  1577.   char *external_string_table;
  1578.   struct linetable **line_vector_all;
  1579.  
  1580.   if (myread (desc, (char *)&hdrr, sizeof hdrr) != sizeof hdrr)
  1581.     return -1;
  1582.  
  1583.   file_descriptor_count = hdrr.ifdMax;
  1584.   line_vector_all = (struct linetable**)
  1585.       alloca (file_descriptor_count * sizeof(struct linetable *));
  1586.   global_symbols_all = (struct pending**)
  1587.       alloca (file_descriptor_count * sizeof(struct pending *));
  1588.   file_symbols_all = (struct pending**)
  1589.       alloca (file_descriptor_count * sizeof(struct pending *));
  1590.   proc_desc_length = hdrr.ipdMax;
  1591.   proc_desc_table = (PDR*)
  1592.     read_table(desc, hdrr.ipdMax * sizeof(PDR), hdrr.cbPdOffset);
  1593.   old_chain = make_cleanup (free_all_symtabs, 0);
  1594.   make_cleanup (free_proc_descs, 0);
  1595.   file_descriptor_table = (FDR*)
  1596.     read_table(desc, hdrr.ifdMax * sizeof(FDR), hdrr.cbFdOffset);
  1597.   file_chain = make_cleanup (free, file_descriptor_table);
  1598.  
  1599.   line_number_table = read_table(desc, hdrr.cbLine, hdrr.cbLineOffset);
  1600.   ext_chain = make_cleanup (free, line_number_table);
  1601.  
  1602.   for (ifile = 0; ifile < hdrr.ifdMax; ifile++)
  1603.     {
  1604.       int iproc, ipdMax;
  1605.       char *cur_line_entry;
  1606.       int cur_addr_offset = 0;
  1607.       int cur_addr;
  1608.  
  1609.       /* Vector of line number information.  */
  1610.       struct linetable *line_vector;
  1611.  
  1612.       /* Index of next entry to go in line_vector_index.  */
  1613.       int line_vector_index;
  1614.  
  1615.       /* Number of elements allocated for line_vector currently.  */
  1616.       int line_vector_length;
  1617.  
  1618.       global_symbols_all[ifile] = 0;
  1619.       file_symbols_all[ifile] = 0;
  1620.       select_file (ifile);
  1621.  
  1622.       /* read line numbers */
  1623.       cur_addr = cur_file_descriptor->adr;
  1624.       iproc = cur_file_descriptor->ipdFirst;
  1625.  
  1626.       line_vector_index = 0;
  1627.       line_vector_length = 1000;
  1628.       line_vector = (struct linetable *)
  1629.       xmalloc (sizeof (struct linetable)
  1630.            + line_vector_length * sizeof (struct linetable_entry));
  1631.  
  1632.       /* we read the line numbers first, since read_symbol over-writes
  1633.      some of the fields in a proc descriptor */
  1634.       ipdMax = iproc + cur_file_descriptor->cpd;
  1635.       cur_line_entry = line_number_table + cur_file_descriptor->cbLineOffset;
  1636.       for ( ; iproc < ipdMax; iproc++) {
  1637.     PDR *proc = &proc_desc_table[iproc];
  1638.     int nlines = (iproc+1 < ipdMax ? (proc+1)->iline : cur_file_descriptor->cline)
  1639.         - proc->iline;
  1640.     int cur_source_line = proc->lnLow;
  1641.     if (dump_stuff)
  1642.         printf(
  1643.         "[proc:%x,framereg:%d,ofset:%d,rmask:%d,flmask:%d,floff:%d,iline:%d,lnLo:%d,lnHi:%d,csl:%d]\n",
  1644.            proc->adr,
  1645.            proc->framereg, proc->frameoffset, proc->regmask,
  1646.            proc->fregmask, proc->fregoffset,
  1647.            proc->iline,proc->lnLow,proc->lnHigh, cur_source_line);
  1648.     if (proc->iline == -1) continue;
  1649.     for (isym = proc->iline; nlines > 0; isym++) {
  1650.       struct linetable_entry *e;
  1651.       int code = *cur_line_entry++;
  1652.       int delta = code >> 4;
  1653.       int count = (code & 15) + 1;
  1654.       if (dump_stuff & 1) printf("%2x", 0xFF & code);
  1655.       if (delta == -8)
  1656.         {
  1657.           if (dump_stuff & 1)
  1658.             printf(" %2x%2x", 0xff & cur_line_entry[0],
  1659.              0xff & cur_line_entry[1]);
  1660.           delta = *cur_line_entry++;
  1661.           delta = (delta << 8) | (unsigned)*cur_line_entry++;
  1662.           isym += 2;
  1663.         }
  1664.       cur_source_line += delta;
  1665.       if (dump_stuff & 1)
  1666.         printf("\t%2d.%2d li:%d %X", delta, count,
  1667.          cur_source_line,
  1668.          cur_file_descriptor->adr + 4 * cur_addr_offset);
  1669.  
  1670.       e = &line_vector->item[line_vector_index];
  1671.       if (line_vector_index == 0 || e[-1].line != cur_source_line)
  1672.         {
  1673.  
  1674.           /* Make sure line vector is big enough.  */
  1675.           if (line_vector_index + 2 >= line_vector_length)
  1676.         {
  1677.           line_vector_length *= 2;
  1678.           line_vector = (struct linetable *)
  1679.             xrealloc (line_vector, sizeof (struct linetable)
  1680.                   + (line_vector_length
  1681.                  * sizeof (struct linetable_entry)));
  1682.           e = &line_vector->item[line_vector_index];
  1683.         }
  1684.  
  1685.           e->line = cur_source_line;
  1686.           e->pc = cur_file_descriptor->adr + 4 * cur_addr_offset;
  1687.           line_vector_index++;
  1688.         }
  1689.  
  1690.       if (delta != 0)
  1691.         {
  1692.           int save_addr = cur_addr;
  1693.           if (cur_addr_offset != 0)
  1694.         {
  1695.           int toffset = (cur_addr_offset - 1) * 4;
  1696.           if (dump_stuff & 1)
  1697.           printf(" [li: %d %x-%x]",
  1698.              cur_source_line, cur_addr, cur_addr+toffset);
  1699.         }
  1700.           cur_addr = save_addr + cur_addr_offset * 4;
  1701.         }
  1702.       cur_addr_offset += count;
  1703.       nlines -= count;
  1704.       if (dump_stuff & 1) putchar('\n');
  1705.     /*  if (delta != 0) { } */
  1706.         }
  1707.       }
  1708.  
  1709.       line_vector->nitems = line_vector_index;
  1710.       line_vector_all[ifile] = (struct linetable *)
  1711.       xrealloc (line_vector, (sizeof (struct linetable)
  1712.                + line_vector_index * sizeof (struct linetable_entry)));
  1713.     }
  1714.   do_cleanups (ext_chain);
  1715.  
  1716.   aux_symbol_table = (AUXU*)
  1717.     read_table(desc, hdrr.iauxMax * sizeof(AUXU), hdrr.cbAuxOffset);
  1718.   make_cleanup (free, aux_symbol_table);
  1719.   rel_file_table = (RFDT*)
  1720.     read_table(desc, hdrr.crfd * sizeof(RFDT), hdrr.cbRfdOffset);
  1721.   make_cleanup (free, rel_file_table);
  1722.   local_symbol_table = (SYMR*)
  1723.     read_table(desc, hdrr.isymMax * sizeof(SYMR), hdrr.cbSymOffset);
  1724.   make_cleanup (free, local_symbol_table);
  1725.   local_string_table = read_table(desc, hdrr.issMax, hdrr.cbSsOffset);
  1726.   make_cleanup (free, local_string_table);
  1727.  
  1728.   external_symbol_table = (EXTR*)
  1729.     read_table(desc, hdrr.iextMax * sizeof(EXTR), hdrr.cbExtOffset);
  1730.   ext_chain = make_cleanup (free, external_symbol_table);
  1731.   external_string_table = read_table(desc, hdrr.issExtMax, hdrr.cbSsExtOffset);
  1732.   make_cleanup (free, external_string_table);
  1733.  
  1734.   /* do the external symbols first */
  1735.   /* The reason is that each file's end_symtab is done with the locals */
  1736.  
  1737.   for (isym = 0; isym < hdrr.iextMax; isym++)
  1738.     {
  1739.       EXTR *ext = &external_symbol_table[isym];
  1740.       SYMR *csym = &ext->asym;
  1741.       char *name = external_string_table+csym->iss;
  1742.       if ((unsigned)ext->ifd >= hdrr.ifdMax) return -1;
  1743.       select_file (ext->ifd);
  1744.       record_misc_function (name, csym->value);
  1745.       if (csym->st != stProc && csym->st != stStaticProc)
  1746.       read_symbol(csym, name);
  1747. #if 1
  1748. if (dump_stuff)
  1749.       printf("EXT[%s,%x,st%s,sc%s,inx:%d,ifd:%d]\n",
  1750.          name, csym->value, MapStNames[csym->st],
  1751.          MapScNames[csym->sc], csym->index, ext->ifd);
  1752. #endif
  1753.     }
  1754.  
  1755.   do_cleanups (ext_chain);
  1756.  
  1757.   for (ifile = 0; ifile < hdrr.ifdMax; ifile++)
  1758.     {
  1759.       select_file (ifile);
  1760.  
  1761.       cur_proc_number = 0;
  1762.       last_end_addr = cur_file_descriptor->adr;
  1763.  
  1764.       start_symtab ();
  1765.  
  1766. if (dump_stuff)
  1767. printf("<FILE.adr:%x,ilB:%d,cb:%d,cbLO:%d,cbL:%d,iSB:%d,cS:%d,nProcs:%d,first:%d>\n",
  1768.        cur_file_descriptor->adr,
  1769.        cur_file_descriptor->ilineBase, cur_file_descriptor->cline,
  1770.        cur_file_descriptor->cbLineOffset, cur_file_descriptor->cbLine,
  1771.        cur_isymBase, cur_file_descriptor->csym,
  1772.        cur_file_descriptor->cpd,
  1773.        cur_file_descriptor->ipdFirst);
  1774.  
  1775.       for (isym = 0; isym < cur_file_descriptor->csym; )
  1776.       {
  1777.         int count = read_symbols(isym);
  1778.         if (count == 0)
  1779.         { /* handle un-handled stEnd */
  1780.           SYMR *sym = &local_symbol_table[cur_isymBase+isym];
  1781.           char *name = &local_string_table[cur_issBase+sym->iss];
  1782. printf("stEnd confusion!");
  1783.           read_symbol(sym, name);
  1784.           count = 1;
  1785.         }
  1786.         isym += count;
  1787.       }
  1788.       end_symtab (cur_file_descriptor->adr, last_end_addr,
  1789.           line_vector_all[ifile]);
  1790.     }
  1791.  
  1792.   do_cleanups (file_chain);
  1793.  
  1794.   last_source_file = 0;
  1795.   bzero (opaque_type_chain, sizeof opaque_type_chain);
  1796.  
  1797.   type_vector_length = 160;
  1798.   type_vector = (struct typevector *)
  1799.         xmalloc (sizeof (struct typevector)
  1800.                 + type_vector_length * sizeof (struct type *));
  1801.   bzero (type_vector->type, type_vector_length * sizeof (struct type *));
  1802.  
  1803.   if (last_source_file)
  1804.     end_symtab ();
  1805.   discard_cleanups (old_chain);
  1806.   return 0;
  1807. }
  1808.  
  1809. /* Routines for reading headers and symbols from executable.  */
  1810.  
  1811. /* Read COFF file header, check magic number,
  1812.    and return number of symbols. */
  1813. read_file_hdr (chan, file_hdr)
  1814.     int chan;
  1815.     FILHDR *file_hdr;
  1816. {
  1817.   lseek (chan, 0L, 0);
  1818.   if (myread (chan, (char *)file_hdr, FILHSZ) < 0)
  1819.     return -1;
  1820.  
  1821.   switch (file_hdr->f_magic)
  1822.     {
  1823. #ifdef MIPSEBMAGIC
  1824.     case MIPSEBMAGIC:
  1825. #endif
  1826. #ifdef MIPSELMAGIC
  1827.     case MIPSELMAGIC:
  1828. #endif
  1829. #ifdef MC68MAGIC
  1830.     case MC68MAGIC:
  1831. #endif
  1832. #ifdef NS32GMAGIC
  1833.       case NS32GMAGIC:
  1834.       case NS32SMAGIC:
  1835. #endif
  1836. #ifdef I386MAGIC
  1837.     case I386MAGIC:
  1838. #endif
  1839. #ifdef CLIPPERMAGIC
  1840.     case CLIPPERMAGIC:
  1841. #endif      
  1842.     return file_hdr->f_nsyms;
  1843.  
  1844.       default:
  1845. #ifdef BADMAG
  1846.     if (BADMAG(file_hdr))
  1847.       return -1;
  1848.     else
  1849.       return file_hdr->f_nsyms;
  1850. #else
  1851.     return -1;
  1852. #endif
  1853.     }
  1854. }
  1855.  
  1856. read_aout_hdr (chan, aout_hdr, size)
  1857.     int chan;
  1858.     AOUTHDR *aout_hdr;
  1859.     int size;
  1860. {
  1861.   lseek (chan, (long)FILHSZ, 0);
  1862.   if (size != sizeof (AOUTHDR))
  1863.     return -1;
  1864.   if (myread (chan, (char *)aout_hdr, size) != size)
  1865.     return -1;
  1866.   return 0;
  1867. }
  1868.  
  1869. read_section_hdr (chan, section_name, section_hdr, nsects)
  1870.     register int chan;
  1871.     register char *section_name;
  1872.     SCNHDR *section_hdr;
  1873.     register int nsects;
  1874. {
  1875.   register int i;
  1876.  
  1877.   if (lseek (chan, FILHSZ + sizeof (AOUTHDR), 0) < 0)
  1878.     return -1;
  1879.  
  1880.   for (i = 0; i < nsects; i++)
  1881.     {
  1882.       if (myread (chan, (char *)section_hdr, SCNHSZ) < 0)
  1883.     return -1;
  1884.       if (strncmp (section_hdr->s_name, section_name, 8) == 0)
  1885.     return 0;
  1886.     }
  1887.     return -1;
  1888. }
  1889.  
  1890.  
  1891. /* Support for string table handling */
  1892.  
  1893. static char *stringtab = NULL;
  1894.  
  1895.  
  1896. #if 0
  1897. static char *
  1898. getsymname (symbol_entry)
  1899.     SYMENT *symbol_entry;
  1900. {
  1901.   static char buffer[SYMNMLEN+1];
  1902.   char *result;
  1903.  
  1904.   if (symbol_entry->n_zeroes == 0)
  1905.     {
  1906.       result = stringtab + symbol_entry->n_offset;
  1907.     }
  1908.   else
  1909.     {
  1910.       strncpy (buffer, symbol_entry->n_name, SYMNMLEN);
  1911.       buffer[SYMNMLEN] = '\0';
  1912.       result = buffer;
  1913.     }
  1914.   return result;
  1915. }
  1916. #endif
  1917.  
  1918. /* Support for line number handling */
  1919. static char *linetab = NULL;
  1920. static long linetab_offset;
  1921. static int linetab_count;
  1922.  
  1923. static int
  1924. hashname (name)
  1925.      char *name;
  1926. {
  1927.   register char *p = name;
  1928.   register int total = p[0];
  1929.   register int c;
  1930.  
  1931.   c = p[1];
  1932.   total += c << 2;
  1933.   if (c)
  1934.     {
  1935.       c = p[2];
  1936.       total += c << 4;
  1937.       if (c)
  1938.     total += p[3] << 6;
  1939.     }
  1940.   
  1941.   return total % HASHSIZE;
  1942. }
  1943.  
  1944. static void
  1945. patch_type (type, real_type)
  1946.     struct type *type;
  1947.     struct type *real_type;
  1948. {
  1949.   register struct type *target = TYPE_TARGET_TYPE (type);
  1950.   register struct type *real_target = TYPE_TARGET_TYPE (real_type);
  1951.   int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
  1952.  
  1953.   TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
  1954.   TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
  1955.   TYPE_FIELDS (target) = (struct field *)
  1956.                 obstack_alloc (symbol_obstack, field_size);
  1957.  
  1958.   bcopy (TYPE_FIELDS (real_target), TYPE_FIELDS (target), field_size);
  1959.  
  1960.   if (TYPE_NAME (real_target))
  1961.     {
  1962.       if (TYPE_NAME (target))
  1963.     free (TYPE_NAME (target));
  1964.       TYPE_NAME (target) = concat (TYPE_NAME (real_target), "", "");
  1965.     }
  1966. }
  1967.  
  1968. /* Patch up all appropriate typdef symbols in the opaque_type_chains
  1969.    so that they can be used to print out opaque data structures properly */
  1970.  
  1971. static void
  1972. patch_opaque_types ()
  1973. {
  1974.   struct symtab *s;
  1975.  
  1976.   /* Look at each symbol in the per-file block of each symtab.  */
  1977.   for (s = symtab_list; s; s = s->next)
  1978.     {
  1979.       register struct block *b;
  1980.       register int i;
  1981.  
  1982.       /* Go through the per-file symbols only */
  1983.       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1);
  1984.       for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
  1985.     {
  1986.       register struct symbol *real_sym;
  1987.  
  1988.       /* Find completed typedefs to use to fix opaque ones.
  1989.          Remove syms from the chain when their types are stored,
  1990.          but search the whole chain, as there may be several syms
  1991.          from different files with the same name.  */
  1992.       real_sym = BLOCK_SYM (b, i);
  1993.       if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
  1994.           SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
  1995.           TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
  1996.           TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
  1997.         {
  1998.           register char *name = SYMBOL_NAME (real_sym);
  1999.           register int hash = hashname (name);
  2000.           register struct symbol *sym, *prev;
  2001.  
  2002.           prev = 0;
  2003.           for (sym = opaque_type_chain[hash]; sym;)
  2004.         {
  2005.           if (name[0] == SYMBOL_NAME (sym)[0] &&
  2006.               !strcmp (name + 1, SYMBOL_NAME (sym) + 1))
  2007.             {
  2008.               if (prev)
  2009.             SYMBOL_VALUE (prev) = SYMBOL_VALUE (sym);
  2010.               else
  2011.             opaque_type_chain[hash]
  2012.               = (struct symbol *) SYMBOL_VALUE (sym);
  2013.  
  2014.               patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
  2015.  
  2016.               if (prev)
  2017.             sym = (struct symbol *) SYMBOL_VALUE (prev);
  2018.               else
  2019.             sym = opaque_type_chain[hash];
  2020.             }
  2021.           else
  2022.             {
  2023.               prev = sym;
  2024.               sym = (struct symbol *) SYMBOL_VALUE (sym);
  2025.             }
  2026.         }
  2027.         }
  2028.     }
  2029.     }
  2030. }
  2031.  
  2032. /* This function is really horrible, but to avoid it, there would need
  2033.    to be more filling in of forward references.  THIS SHOULD BE MOVED
  2034.    OUT OF COFFREAD.C AND DBXREAD.C TO SOME PLACE WHERE IT CAN BE SHARED. */
  2035. int
  2036. fill_in_vptr_fieldno (type)
  2037.      struct type *type;
  2038. {
  2039.   if (TYPE_VPTR_FIELDNO (type) < 0)
  2040.     TYPE_VPTR_FIELDNO (type) =
  2041.       fill_in_vptr_fieldno (TYPE_BASECLASS (type, 1));
  2042.   return TYPE_VPTR_FIELDNO (type);
  2043. }
  2044.  
  2045. /* partial symbol tables are not implemented in coff, therefore
  2046.    block_for_pc() (and others) will never decide to call this. */
  2047.  
  2048. extern struct symtab *
  2049. psymtab_to_symtab ()
  2050. {
  2051.   fatal ("error: Someone called psymtab_to_symtab\n");
  2052. }
  2053.  
  2054. /* These will stay zero all the time */
  2055. struct psymbol_allocation_list global_psymbols, static_psymbols;
  2056.